﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using StockLib;
using StockUtil;

namespace StockControl
{
    public partial class StockKGraphControl : Control
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left | Keys.Shift:
                    ScrollLeft(1);
                    return true;

                case Keys.Right | Keys.Shift:
                    ScrollRight(1);
                    return true;

                case Keys.Left | Keys.Shift | Keys.Control:
                    ScrollLeft(5);
                    return true;

                case Keys.Right | Keys.Shift | Keys.Control:
                    ScrollRight(5);
                    return true;

                case Keys.Left:
                    return true;

                case Keys.Right:
                    return true;

                case Keys.Up:
                    ExpandUp();
                    return true;

                case Keys.Down:
                    ExpandDown();
                    return true;

                case Keys.Tab:
                    kIndicator.showIndicator = !kIndicator.showIndicator;
                    Invalidate();
                    return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        float expandRatio = 0.3f;
        public void ExpandDown()
        {
            int indexStartOld = indexStart;
            indexStart -= (int)(expandRatio * (indexEnd - indexStart) / (1 - expandRatio));
            if (indexStartOld != indexStart)
            {
                Invalidate();
                Release();
            }
        }

        public void ExpandUp()
        {
            int indexStartOld = indexStart;
            indexStart += (int)(expandRatio * (indexEnd - indexStart));
            if (indexStartOld != indexStart)
            {
                Invalidate();
                Release();
            }
        }

        public KLineType KLineType
        {
            get
            {
                return this.kIndicator.klineType;
            }
            set
            {
                this.kIndicator.klineType = value;
                this.Invalidate(kIndicator.rectGraph);
            }
        }

        private void ScrollLeft(int days)
        {
            int indexStartOld = indexStart;
            indexStart -= days;
            indexEnd -= days;
            if (indexStartOld != indexStart)
            {
                Invalidate();
                Release();
            }
        }

        private void ScrollRight(int days)
        {
            int indexStartOld = indexStart;
            indexStart += days;
            indexEnd += days;
            if (indexStartOld != indexStart)
            {
                Invalidate();
                Release();
            }
        }
    }
}
